package manage.actions;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import manage.commons.annontations.RequiredLogin;
import manage.commons.annontations.RequiredLogin.Required;
import manage.commons.utils.InputStreamUtil;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 时间工具类
 * 
 * @author dongji
 * @since 2012-11-9 下午04:55:52
 */
@RequiredLogin(Required.FALSE)
public class CurrentTimeAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private String format;
	private InputStream inputStream;
	private Map<String, Object> jsonMap = new HashMap<String, Object>();;

	@Override
	@Action(value = "get-current-time", results = { @Result(name = SUCCESS, type = "stream", params = {
			"contentType", "text/plain; charset=utf-8", "inputName",
			"inputStream" }) })
	public String execute() throws Exception {
		if (StringUtils.isBlank(format)) {
			format = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		inputStream = InputStreamUtil.toInputStream(sdf.format(new Date()));
		return SUCCESS;
	}

	@Action(value = "get-current-millis", results = { @Result(name = SUCCESS, type = "json", params = {
			"root", "jsonMap" }) })
	public String currentTimeMillis() {
		jsonMap.put("millis", System.currentTimeMillis());
		return SUCCESS;
	}

	public String getFormat() {
		return format;
	}

	public void setFormat(String format) {
		this.format = format;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public Map<String, Object> getJsonMap() {
		return jsonMap;
	}
}
